package gti310.tp4;

public class ColorConvert {

	public static int[][][] rgbyul(int[][][] imageRgb) {

		int R, G, B, Y, U, V;

		int[][][] YUV = new int[imageRgb.length][imageRgb[0].length][imageRgb[0][0].length];

		for (int i = 0; i < imageRgb[0].length; i++) {
			for (int j = 0; j < imageRgb[0][0].length; j++) {
				R = imageRgb[Main.R][i][j];
				G = imageRgb[Main.G][i][j];
				B = imageRgb[Main.B][i][j];

				Y = (int) (0.299 * R + 0.587 * G + 0.114 * B);
				U = (int) (-0.16874 * R - 0.33126 * G + 0.50000 * B);
				V = (int) (0.50000 * R - 0.41869 * G - 0.08131 * B);

				YUV[Main.Y][i][j] = Y;
				YUV[Main.U][i][j] = U;
				YUV[Main.V][i][j] = V;

			}
		}
		return YUV;
	}

	public static int[][][] yulrgb(int[][][] imageYuv) {

		int R, G, B, Y, U, V;

		int[][][] RGB = new int[imageYuv.length][imageYuv[0].length][imageYuv[0][0].length];

		for (int i = 0; i < imageYuv[0].length; i++) {
			for (int j = 0; j < imageYuv[0][0].length; j++) {
				Y = imageYuv[Main.Y][i][j];
				U = imageYuv[Main.U][i][j];
				V = imageYuv[Main.V][i][j];

				R = (int) (Y + 1.403 * V);
				G = (int) (Y - 0.344 * U - 0.714 * V);
				B = (int) (Y + 1.770 * U);

				if (R > 255)
					R = 255;
				if (R < 0)
					R = 0;
				if (G > 255)
					G = 255;
				if (G < 0)
					G = 0;
				if (B > 255)
					B = 255;
				if (B < 0)
					B = 0;

				RGB[Main.R][i][j] = R;
				RGB[Main.G][i][j] = G;
				RGB[Main.B][i][j] = B;

			}
		}
		return RGB;
	}

}
